package com.lzx.lizhenxing.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzx.lizhenxing.common.exception.CommonException;
import com.lzx.lizhenxing.common.utils.UserUtils;
import com.lzx.lizhenxing.common.vo.PageVo;
import com.lzx.lizhenxing.sys.dao.UserDao;
import com.lzx.lizhenxing.sys.model.User;
import com.lzx.lizhenxing.user.dao.ExpressDeliveryDao;
import com.lzx.lizhenxing.user.dao.UserExpressDeliveryDao;
import com.lzx.lizhenxing.user.model.ExpressDeliveryModel;
import com.lzx.lizhenxing.user.model.UserExpressDeliveryModel;
import com.lzx.lizhenxing.user.param.ExpressDeAddParam;
import com.lzx.lizhenxing.user.param.ExpressDePageQueryParam;
import com.lzx.lizhenxing.user.service.UserExpressDeliveryService;
import com.lzx.lizhenxing.user.vo.ExpressDePageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class UserExpressDeliveryServiceImpl implements UserExpressDeliveryService {

    @Resource
    private UserExpressDeliveryDao userExpressDeliveryDao;

    @Resource
    private UserDao userDao;

    @Resource
    private ExpressDeliveryDao expressDeliveryDao;

    @Override
    public PageVo<ExpressDePageVo> queryByPage(ExpressDePageQueryParam param) {
        Long uid = UserUtils.getUser();
        // 构建模糊搜索条件（订单编码、快递公司）
        LambdaQueryWrapper<UserExpressDeliveryModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserExpressDeliveryModel::getUserId, uid);
        if (!Objects.isNull(param.getCourierCompany())) {
            queryWrapper.like(UserExpressDeliveryModel::getCourierCompany, param.getCourierCompany());
        }
        if (!Objects.isNull(param.getBillNum())) {
            queryWrapper.like(UserExpressDeliveryModel::getBillNum, param.getBillNum());
        }
        // 构建分页条件
        Page<UserExpressDeliveryModel> exDePage = new Page<>(param.getPageNum(), param.getPageSize());
        // 执行查询sql，会在mybatis分页插件配置中为sql拼上limit条件
        IPage<UserExpressDeliveryModel> exDeIPage = userExpressDeliveryDao.selectPage(exDePage, queryWrapper);
        PageVo<ExpressDePageVo> pageVo = new PageVo<>();
        // 查到的数据
        User user = userDao.selectById(uid);
        if(CollectionUtils.isEmpty(exDeIPage.getRecords())){
            return new PageVo<>( 0 , new ArrayList<>());
        }
        List<UserExpressDeliveryModel> records = exDeIPage.getRecords();
        // 转换为vo对象，返回给前端
        List<ExpressDePageVo> voList = new ArrayList<>();
        records.forEach( e -> {
            ExpressDePageVo vo = new ExpressDePageVo();
            BeanUtils.copyProperties( e , vo );
            vo.setPhone(user.getPhone());
            voList.add(vo);
        });
        pageVo.setDataList(voList);
        // 数据条数
        pageVo.setTotal(exDeIPage.getTotal());
        return pageVo;
    }

    @Override
    public void deleteExDeBatch(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        userExpressDeliveryDao.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = CommonException.class)
    public void addExDe(ExpressDeAddParam param) {

        String billNum = param.getBillNum();
        UserExpressDeliveryModel model = userExpressDeliveryDao.selectByBillNum(billNum);
        if(Objects.nonNull(model)){
            throw new CommonException("该快递已添加,请勿重复添加");
        }
        // 如果快递库没有该快递，也不能添加
        ExpressDeliveryModel exDeModel = expressDeliveryDao.selectByBillNum(billNum);
        if(Objects.isNull(exDeModel)){
            throw new CommonException("请输入正确的快递编码");
        }
        if(!Objects.equals(exDeModel.getPhone(),param.getPhone())){
            throw new CommonException("只能添加本人快递，该快递不是本人的");
        }
        UserExpressDeliveryModel addModel = new UserExpressDeliveryModel();
        BeanUtils.copyProperties(param, addModel);
        addModel.setUserId(UserUtils.getUser());
        userExpressDeliveryDao.insert(addModel);
    }

    @Override
    @Transactional(rollbackFor = CommonException.class)
    public void deleteOneExDe(Long id) {
        userExpressDeliveryDao.deleteById(id);
    }

    @Override
    public void setExpressDeliveryStatus(Long id, Integer status) {

        UserExpressDeliveryModel deliveryModel = userExpressDeliveryDao.selectById(id);
        if(Objects.isNull(deliveryModel)){
            return;
        }
        deliveryModel.setStatus(status);
        userExpressDeliveryDao.updateById(deliveryModel);
    }
}
